package com.hanson.stream.test;

import org.junit.jupiter.api.Test;

import java.util.*;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * @author Hanson
 * @date 2023/1/13 10:39
 */
public class Streams {

    @Test
    public void test1() {
//        String[] strs = new String[]{"hello", "world", "hi", "com", "student"};
        //基于collection集合生成流
        List<String> list = new ArrayList<>();
        list.add("hello");
        list.add("world");
        list.add("hi");
        list.add("com");
        list.add("student");
        //调用stream方法生成流对象
        Stream<String> stream = list.stream();
        stream.forEach(System.out::println);
        //一般开发中会直接调用，链式编程
        list.stream().forEach(System.out::println);
        // System.out::println写法是下面方法的简化
        list.stream().forEach(x -> System.out.println(x));
    }

    @Test
    public void test2() {
//        String[] strs = new String[]{"hello", "world", "hi", "com", "student"};
        Map<String,String> map = new HashMap<>();
        map.put("1","hello");
        map.put("2","world");
        map.put("3","hi");
        map.put("4","com");
        map.put("5","student");
        //map是通过对key集合、或value集合生成流对象
        Set<String> keySet = map.keySet();
        keySet.stream().forEach(x->{
            System.out.println(x+":value:"+map.get(x));
        });
        //取出map中values作为集合对象
        Collection<String> values = map.values();
        values.stream().forEach(System.out::println);
    }

    @Test
    public void test3() {
        String[] strs = new String[]{"hello", "world", "hi", "com", "student"};
        Stream<String> stream1 = Arrays.stream(strs);
        Stream<String> strs1 = Stream.of(strs);
        strs1.forEach(System.out::println);
        //int数组返回的两种流形式不一致
        int[] ints = new int[]{1,2,3,4,5};
        IntStream stream = Arrays.stream(ints);
        Stream<int[]> ints1 = Stream.of(ints);
    }

    /**
     * filter():过滤方法：
     * filter 操作
     */
    @Test
    public void test4() {
        String[] strs = new String[]{"hello", "world", "hi", "com", "student"};
        Stream<String> strs1 = Stream.of(strs);
        //Stream<String> stringStream = strs1.filter(x->x.length()>4 && x.length()<6);
        //stringStream.forEach(System.out::println);
        //第二种方式
        Stream<String> stringStream1 = strs1.filter(x -> x.length() > 4).filter(x -> x.length() < 6);
        //stringStream.forEach(System.out::println);
        stringStream1.forEach(System.out::println);
    }

    @Test
    public void test5() {
        String[] strs = new String[]{"hello", "world", "hi", "com", "student"};
        Stream<String> strs1 = Stream.of(strs); //基于String的流
        Stream<Integer> integerStream = strs1.map(x -> x.length()); //经过map转换后得到的一个新流
        integerStream.forEach(System.out::println);

    }

    /**
     * 返回Map形式的流
     */
    @Test
    public void test6() {
        String[] strs = new String[]{"hello", "world", "hi", "com", "student"};
        Stream<String> strs1 = Stream.of(strs); //基于String的流
//        strs1.map(x->{
//            Map<String,String> map= new HashMap<>();
//            map.put(x.toUpperCase(),x);
//            return map;
//        }).forEach(System.out::println);

        strs1.map(x->{
            Map<String,String> map= new HashMap<>();
            map.put(x.toUpperCase(),x);
            return map;
        }).forEach(System.out::println);

    }

    @Test
    public void test7() {
        List<StringBuffer> strs = new ArrayList<>();
        strs.add(new StringBuffer("hello"));
        strs.add(new StringBuffer("world"));
        strs.add(new StringBuffer("hi"));
        strs.add(new StringBuffer("com"));
        strs.add(new StringBuffer("student"));
        List<StringBuffer> strs2 = new ArrayList<>();
        strs2.add(new StringBuffer("hello"));
        strs2.add(new StringBuffer("world"));
        strs2.add(new StringBuffer("hi"));
        strs2.add(new StringBuffer("com"));
        strs2.add(new StringBuffer("student"));
        List<List<StringBuffer>> allList = new ArrayList<>();
        allList.add(strs2);
        allList.add(strs);
        Stream<Stream<StringBuffer>> streamStream = allList.stream().map(x -> x.stream().map(item -> {
            if (item.length() > 3) {
                item.append("大于三");
            }
            return item;
        }));
        streamStream.forEach(x->x.forEach(i->System.out.println(i)));
    }








}
